/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package YottaTron.Strategies;

import YottaTron.Behaviors.GoToBehavior;
import YottaTron.Runner;
import YottaTron.Sprites.BotSprite;
import YottaTron.Sprites.Sprite;
import java.awt.Point;

/**
 *
 * @author Cenda
 */
public class RandomStrategy extends Strategy {
    int antiBlock = 0;
    int maxIterations = 100;

    public RandomStrategy(Sprite sprite, Runner runner) {
        this.sprite = sprite;
        this.runner = runner;
    }

    public void checkBehavior() {
        if (sprite.getAction() == null || antiBlock>1000) {
            antiBlock = 0;
            Point destination = findClearWay();
            sprite.behavior = new GoToBehavior((BotSprite)sprite, destination.x,destination.y, runner);
        }else{
        antiBlock++;
        }
    }

    public Point findClearWay() {
        Point destination = new Point();
        boolean found = false;
        Point from = new Point(runner.XToTiles((int) sprite.getCenterX()), runner.YToTiles((int) sprite.getCenterY()));
        double randomDirection = Math.random() * Math.PI * 2;
        int randomDestination = (int) (Math.random() * 10);
        double dx = Math.cos(randomDirection);
        double dy = Math.sin(randomDirection);
        int step = 1;
        Point nextPlace = from;
        int iterations=0;
        while (!found) {
            nextPlace = new Point(from.x + (int) (dx * step), from.y + (int) (dy * step));
            
            //pokud je v ceste prekazka zkusime jinou cestu
            if (runner.herniPole[nextPlace.x][nextPlace.y] != 0) {
                randomDirection = Math.random() * Math.PI * 2;
                randomDestination = (int) Math.random() * 10;
                dx = Math.cos(randomDirection);
                dy = Math.sin(randomDirection);
                step = 1;
            }
            if(step==randomDestination){
            found = true;
                System.out.println("NALEZENO");
            destination.x = runner.tilesToX(nextPlace.x);
            destination.y = runner.tilesToY(nextPlace.y);
                System.out.println(destination);
            }
            
            step++;
            iterations++;
            if(iterations==maxIterations){
            found = true;
            destination = from;
            }
        }
        return destination;
    }
}
